import { Image } from 'ant-design-vue'
import { defineClientConfig } from '@vuepress/client'
import 'ant-design-vue/lib/image/style/index.css'

function hashString(str) {
  let hash = 0
  for (let i = 0; i < str.length; i++) {
    hash = ((hash << 5) - hash) + str.charCodeAt(i)
    hash |= 0 // Convert to 32-bit integer
  }
  return hash.toString()
}
export default defineClientConfig({
  async enhance({ app, router, siteData }) {
    app.use(Image)

    await import('../../packages/index').then(module => {
      const WattPCUI = module.default
      app.use(WattPCUI)
    })

    // 客户端路由守卫
    if (!__VUEPRESS_SSR__) {
      if (typeof window.global === 'undefined') {
        window.global = window
      }
      await import('@wattjs/network').then(module => {
        const Network = module.default

        app.provide('$network', Network)
        app.provide('$model', module.Model)
      })
      await import('@wattjs/peach').then(module => {
        const Peach = module.default

        // 生产上报
        if (!__VUEPRESS_DEV__) {
          router.beforeEach((to, from, next) => {
            const host = window.location.origin + '/pc'

            // 百度统计
            if (typeof _hmt !== "undefined") {
              if (to.path) {
                _hmt.push(["_trackPageview", `${host}${to.fullPath}`]);
              }
            }

            // 上报uv
            if (to.name !== from.name) {
              const path = `${host}${to.fullPath}`
              Peach.getInstance().logPage({name: hashString(path), path})
            }
            next()
          })
        }
      })
    }

  },
  setup() {},
  rootComponents: [],
})
